package com.airbnb.mvrx.sample.features.book.models

import android.support.v4.app.FragmentActivity
import com.airbnb.mvrx.BaseMvRxViewModel
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.sample.Utils
import com.airbnb.mvrx.sample.core.MvRxViewModel
import com.airbnb.mvrx.sample.models.RespState
import com.airbnb.mvrx.sample.network.BooklnService
import org.json.JSONObject
import org.koin.android.ext.android.inject

class RecommendLessonViewModel(initialState: RespState, private val booklnService: BooklnService) : MvRxViewModel<RespState>(initialState) {

    init {
        fetchLessonRecommend()
    }

    private fun fetchLessonRecommend() = withState {
        val map: MutableMap<String, Any> = mutableMapOf()
        map.put("_data", JSONObject()
                .put("targetId", 26842)
                .put("targetType", 1)
                .toString())
        map.putAll(Utils.getCommonParam())
        booklnService.fetchLessonRecommend(map).execute { copy(resp = it) }
    }

    companion object : MvRxViewModelFactory<RespState> {
        @JvmStatic
        override fun create(activity: FragmentActivity, state: RespState)
                : BaseMvRxViewModel<RespState> {
            val service: BooklnService by activity.inject()
            return RecommendLessonViewModel(state, service)
        }
    }
}